home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group99a.txt / 000220_icon-group-sender _Thu Oct 21 15:50:06 1999.msg < prev    next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id PAA05811
  4.     for icon-group-addresses; Thu, 21 Oct 1999 15:49:56 -0700 (MST)
  5. Message-Id: <199910212249.PAA05811@baskerville.CS.Arizona.EDU>
  6. From: gep2@terabites.com
  7. Date: Thu, 21 Oct 1999 13:52:24 -0500
  8. Subject: List subscription insert/removal
  9. To: icon-group@optima.CS.Arizona.EDU
  10. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  11. Status: RO
  12.  
  13. > I am trying to figure out how to insert a new element into a list,
  14. and remove an element from the middle of a list (not the end).
  15. In answer to this question previously Gordon Peterson sent
  16. a reply.  I tried what he said and it doesn't work.
  17.  
  18. It does if you do it right.  :-)
  19.  
  20. > My original question, that he answered, was
  21. "I find no routines to pluck one element out of the middle of a list, or insert
  22. one in.  Are there such routines?".
  23.  
  24. > His answer was:
  25. "Perhaps not, since those functions are really pretty trivial (you can use
  26. subscript ranges to specify sublists, making that kind of thing a simple
  27. expression).".
  28.  
  29. > I assume he meant that I can insert an element into a list the same way
  30. I can insert a character into a string variable.  So I tried it, 4 ways; none
  31. of them worked, they all produce a run-time error and abort.  Here is
  32. the program.  Though I use mere charactes or 1-char strings as list
  33. elements, it is just to try out list manipulation...
  34.  
  35. > procedure main()
  36.  local s  # it will be a list
  37.  
  38. >  s:= ["a", "b", "c", "d"]
  39.  WriteL(s)   # this writes "abcd" as it should
  40.   # Next, I want to insert "x" as a list element between "b" and "c" ....
  41.   # didn't work:  s[3:3]:="x"
  42.   # didn't work:  s[3:3] := ["x"]
  43.   # didn't work:  s[3+:0] := ["x"]
  44.   s[3+:0] := "x"   # didn't work; I get a run-time error and program abort
  45.  WriteL(s)
  46. end
  47.  
  48. > procedure WriteL(L)
  49.   local i, len
  50.   len := *L
  51.   i := 1
  52.   while  i <= len do  { writes(L[i])  ;  i +:= 1 }
  53.   write()
  54. end
  55.  
  56. > I get the following error message when I run the above:
  57. Run-time error 111
  58. File E:\icon\wsource\sublist.icn; Line 28   [the line:   s[3+:0] := "x" ]
  59. variable expected
  60. offending value: list_3 = []
  61. Traceback:
  62.    main()
  63.    {list_3 = [] := "x"} from line 28 in E:\icon\wsource\sublist.icn
  64.  
  65. > Can you tell me how to accomplish what I want?
  66.  
  67. Try sectioning the list and concatenating (if you have the Icon book, 1st 
  68. edition, this is on pages 52 and 53).  In particular, for your test example 
  69. above, the code might look like:
  70.  
  71.    s := s[1:2] ||| ["x"] ||| s[3:0]
  72.  
  73. The book states (page 53, but anyhow in the description of List Sections):  "In 
  74. particular, a[i:j] is a list that is distinct from 'a', and assignment cannot be 
  75. made to it to change 'a'."
  76.  
  77. If you don't have a copy of the Icon book, you *really* ought to get a copy.  It 
  78. really is VERY important if you plan to make effective use of the language, and 
  79. will pretty instantly answer most of this kind of question.
  80.  
  81. Gordon Peterson
  82. http://web2.airmail.net/gep2/
  83. Support the Anti-SPAM Amendment!  Join at http://www.cauce.org/
  84. 12/19/98: the day the Conservatives demonstrated their scorn for their
  85.    fraudulent sham of representative government.  Voters, remember it!
  86.  
  87.